/*
* @Author: Josh
* @Date:   2019-01-25 21:13:11
* @Last Modified by:   Administrator
* @Last Modified time: 2019-04-13 20:38:55
*/
'use strict'

require('./index.css');
require('page/common/nav-simple/index.js');
var _mall = require('util/mall.js'),
    _user = require('service/user-service.js');

//表单里的错误提示
var formError = {
    show : function(errMsg){
        $('.error-item').show().find('.err-msg').text(errMsg);
    },
    hide : function(){
        $('.error-item').hide().find('.err-msg').text('');
    }
};

//page 逻辑部分
var page = {
    init : function(){
        this.bindEvent();
    },
    bindEvent : function(){
        var _this = this;
        //验证用户名是否存在
        $('#username').blur(function(){
            var username = $.trim($(this).val());
            //用户名为空
            if(!username){
                return;
            }
            // 异步验证
            _user.checkUsername(username, function(res){
                formError.hide();
            }, function(errMsg){
                formError.show(errMsg);
            });
        });
        //注册按钮点击事件
        $('#submit').click(function(){
            _this.submit();
        });
        //键盘回车输入事件
        $('.user-content').keyup(function(e){
            //keyCode为回车键
            if(e.keyCode === 13){
                _this.submit();
            }
        });
    },
    //提交表单
    submit : function(who){
        //取得表单数据
        var formData = {
                username        : $.trim($('#username').val()),
                password        : $.trim($('#password').val()),
                passwordConfirm : $.trim($('#password-confirm').val()),
                phone           : $.trim($('#phone').val()),
                email           : $.trim($('#email').val()),
                question        : $.trim($('#question').val()),
                answer          : $.trim($('#answer').val())
        };
        //查看验证结果
        var validatrResult = this.formValidate(formData);

        //验证成功
        if(validatrResult.status){
            _user.register(formData, function(res){
                window.location.href = './result.html?type=register';
            }, function(errMsg){
                formError.show(errMsg);
            });
        }
        //验证失败
        else{
            //错误提示
            formError.show(validatrResult.msg);
        }

    },
    //表单字段的验证
    formValidate : function(formData){
        var result = {
            status : false,
            msg    : '',
        };
        //密码或用户名为空判断
        if(!_mall.validate(formData.username, 'require')){
            result.msg = '请输入用户名！';
        }
        else if(!_mall.validate(formData.password, 'require')){
            result.msg = '请输入密码！';
        }
        //验证密码长度
        else if(formData.password.length < 6){
            result.msg = '密码长度不能少于6位！';
        }
        //验证确认密码是否正确
        else if(formData.password !== formData.passwordConfirm){
            result.msg = '两次输入的密码不一致！';
        }
        //验证手机号码
        else if(!_mall.validate(formData.phone, 'phone')){
            result.msg = '请输入正确的手机号码！';
        }
        //验证邮箱号码
        else if(!_mall.validate(formData.email, 'email')){
            result.msg = '请输入正确的邮箱号码！';
        }
        //验证密码找回问题是否为空
        else if(!_mall.validate(formData.question, 'require')){
            result.msg = '请输入密码找回问题！';
        }
        ////验证密码找回问题答案是否为空
        else if(!_mall.validate(formData.answer, 'require')){
            result.msg = '请输入密码找回问题答案！';
        }
        else{
            //通过验证，返回正确提示
            result.status = true;
            result.msg    = '验证成功';
        }
        return result;
    }
};

$(function(){
    page.init(); 
});